package com.ruoyi.controller;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.entity.AttendanceQueryDTO;
import com.ruoyi.entity.AttendanceRecord;
import com.ruoyi.entity.AttendanceVO;
import com.ruoyi.mapper.AttendanceMapper;
import com.ruoyi.service.impl.AttendanceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.List;

/**
 * @Classname AttendanceImpl
 * @Description
 * @Date 2025/11/1 14:14
 * @Created by SYL
 */

@RestController
@RequestMapping("/attendance")
public class AttendanceConteoller extends BaseController {
            @Autowired
            private AttendanceMapper attendanceMapper;



        public AjaxResult list() {
             System.out.println("执行list 搜索参数:{}");
             List<AttendanceRecord> list = attendanceMapper.selectList();
            System.out.println("list = " + list);
            return success(list);
        }


        /**
         * 查询考勤打卡记录VO列表
         *
         * @param queryDTO 考勤查询参数
         * @return 考勤打卡记录VO集合
         */
    @GetMapping("/List")
    public TableDataInfo voList(AttendanceQueryDTO queryDTO) {
        System.out.println("执行voList 搜索参数:" + queryDTO);

        // 直接使用 PageHelper 的分页方法
        Integer pageNum = queryDTO.getPageNum() != null ? queryDTO.getPageNum() : 1;
        Integer pageSize = queryDTO.getPageSize() != null ? queryDTO.getPageSize() : 5;

        // PageHelper 分页设置
        com.github.pagehelper.PageHelper.startPage(pageNum, pageSize, true);


        // 提取时间范围
        String[] checkTime = queryDTO.getCheckTime();
        String startTime = checkTime != null && checkTime.length > 0 ? checkTime[0] : null;
        String endTime = checkTime != null && checkTime.length > 1 ? checkTime[1] : null;

        //不会若依权限管理
//        String currentUsername = getCurrentUsername();
//        String string = attendanceMapper.selectUserIdByUsername(currentUsername);
//        if (!string.equals(1)) {
//            queryDTO.setUserId(string);
//        }

        // 构造查询条件
         List<AttendanceVO> list = attendanceMapper.selectVOlistByCondition(queryDTO.getUserId(), startTime, endTime);

        System.out.println("list分页后 = " + list);
        return getDataTable(list);
    }



    @Autowired
    private AttendanceImpl attendanceImpl;

    @GetMapping("/dak")
    public AjaxResult dak(){
        System.out.println("执行dak 打卡");
        // 从上下文中获取当前认证信息
        String username = getCurrentUsername();
        if (username == null) {
            return error("用户未登录");
        }
        // 调用服务层方法执行打卡操作
        int result = attendanceImpl.executeCheckIn(username);

        if (result == 1) {
            return success("打卡成功");
        } else if (result == 2) {
            return error("您已打过卡，无需重复打卡");
        } else {
            return error("打卡失败");
        }
    }
    public String getCurrentUsername() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();//可以获取当前用户的认证信息
        String username;
        // 检查用户是否已认证
        if (authentication != null) {
            // 获取用户权限列表
            Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
            System.out.println("authorities = " + authorities);

            username = authentication.getName(); // 获取用户名
            System.out.println("用户名username = " + username);
        } else {
            System.out.println("用户未登录");
            return null;
        }
        return username;
    }

}
